﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Threading.Tasks;
using Hourglass.Qrobot.Models;
using Hourglass.Qrobot.Utils;

namespace Hourglass.Qrobot.Http
{
    public static class DataProvider
    {
        /// <summary>
        /// 获取用于登录的二维码
        /// </summary>
        public static Image GetLoginQRCode(LoginQRCodeRequest para)
        {
            var url = string.Format(QrobotConsts.URL_GetLoginQRCode, para.AppId, para.RandomDouble);
            var request = new HttpRequestHandler(url);
            request.Referer = string.Format(QrobotConsts.URL_LoginQRCodeReferer, para.AppId);
            var image = request.RequestImage(para.CookieContainer);
            return image;
        }

        /// <summary>
        /// 获取扫描登录二维码的状态
        /// </summary>
        public static string GetLoginScanStatus(LoginScanStatusRequest para)
        {
            var url = string.Format(QrobotConsts.URL_LoginQRCodeState, para.AppId, para.RandomInt);
            var request = new HttpRequestHandler(url);
            request.Referer = string.Format(QrobotConsts.URL_LoginQRCodeReferer, para.AppId);
            var response = request.RequestString(para.CookieContainer);
            return response;
        }

        /// <summary>
        /// 验证，返回值为0，但会回写一些cookie
        /// </summary>
        public static void LoginSigCheck(LoginSigCheckRequest para)
        {
            var request = new HttpRequestHandler(para.Url);
            request.RequestString(para.CookieContainer);
        }

        /// <summary>
        /// 获取登录后的vfwebqq字段
        /// </summary>
        public static string GetLoginVfwebqq(LoginVfwebqqRequest para)
        {
            var url = string.Format(QrobotConsts.URL_Getvfwebqq, para.ClientId, para.Ptwebqq, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<dynamic>(para.CookieContainer);
            return response.result.vfwebqq;
        }

        /// <summary>
        /// 获取登录后的psessionid和登录者QQ号
        /// </summary>
        public static ReturnedLoginedPsessionid GetLoginPsessionidWithQq(LoginPsessionRequest para)
        {
            var parameter = string.Format(QrobotConsts.PARA_Psessionid, para.ClientId, para.Ptwebqq);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(QrobotConsts.URL_Psessionid);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.ApplyV2Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedLoginedPsessionid>>(para.CookieContainer);
            return response.Result;
        }

        /// <summary>
        /// 根据个人ID获取QQ号
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        public static long GetPersonalQq(PersonalQqRequest para)
        {
            return GetAccountNumberById(para);
        }

        /// <summary>
        /// 根据GroupCode获取群号
        /// </summary>
        public static long GetGroupNumber(GroupNumberRequest para)
        {
            return GetAccountNumberById(para);
        }

        /// <summary>
        /// 根据个人ID获取QQ号，或根据群组Code获取群号
        /// </summary>
        private static long GetAccountNumberById(AccountNumberRequest para)
        {
            var url = string.Format(QrobotConsts.URL_GetAccountNumber, para.IdOrCode, para.Vfwebqq, para.Milliscond, para.Type);
            var request = new HttpRequestHandler(url);
            var response = request.RequestJsonModel<BaseResponse<ReturnedAccountInfo>>(para.CookieContainer);
            return response.Result.Account;
        }

        /// <summary>
        /// 获取好友列表
        /// </summary>
        public static ReturnedUserFriends LoadFriends(LoadFriendRequest para)
        {
            var parameter = string.Format(QrobotConsts.PARA_Friends, para.Vfwebqq, para.QHash);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(QrobotConsts.URL_Friends);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedUserFriends>>(para.CookieContainer);
            return response.Result ?? ReturnedUserFriends.EmptyNew();
        }

        /// <summary>
        /// 获取QQ群列表
        /// </summary>
        public static ReturnedGroups LoadGroups(LoadGroupRequest para)
        {
            var parameter = string.Format(QrobotConsts.PARA_Groups, para.Vfwebqq, para.QHash);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(QrobotConsts.URL_Groups);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedGroups>>(para.CookieContainer);
            return response.Result ?? ReturnedGroups.EmptyNew();
        }

        /// <summary>
        /// 获取群聊列表
        /// </summary>
        public static ReturnedDisscusses LoadDisscusses(LoadDisscussRequest para)
        {
            var url = string.Format(QrobotConsts.URL_Disscuss, para.ClientId, para.Psessionid, para.Vfwebqq, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedDisscusses>>(para.CookieContainer);
            return response.Result ?? ReturnedDisscusses.EmptyNew();
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        public static ReturnedUserInfo LoadOwnerInfo(LoadOwnerInfoRequest para)
        {
            var url = string.Format(QrobotConsts.URL_OwnerInfo, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedUserInfo>>(para.CookieContainer);
            return response.Result;
        }

        /// <summary>
        /// 获取最近联系人
        /// </summary>
        public static List<ReturnedRecent> LoadRecentFriends(LoadRecentRequest para)
        {
            var parameter = string.Format(QrobotConsts.PARA_Recent, para.ClientId, para.Vfwebqq, para.Psessionid);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(QrobotConsts.URL_Recent);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.ApplyV2Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<List<ReturnedRecent>>>(para.CookieContainer);
            return response.Result ?? new List<ReturnedRecent>();
        }

        /// <summary>
        /// 获取在线好友列表
        /// </summary>
        public static List<ReturnedOnlineFriend> LoadOnlineFriends(LoadOnlineRequest para)
        {
            var url = string.Format(QrobotConsts.URL_OnlineFirends, para.ClientId, para.Vfwebqq, para.Psessionid, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV2Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<List<ReturnedOnlineFriend>>>(para.CookieContainer);
            return response.Result ?? new List<ReturnedOnlineFriend>();
        }

        /// <summary>
        /// 获取单个好友详细信息
        /// </summary>
        public static ReturnedFriendInfo LoadFriendInfo(LoadFriendInfoRequest para)
        {
            var url = string.Format(QrobotConsts.URL_FriendInfo, para.FriendId, para.Vfwebqq, para.ClientId, para.Psessionid, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedFriendInfo>>(para.CookieContainer);
            return response.Result;
        }

        /// <summary>
        /// 获取单个群组详细信息
        /// </summary>
        public static ReturnedGroupInfo LoadGroupInfo(LoadGroupInfoRequest para)
        {
            var url = string.Format(QrobotConsts.URL_GroupInfo, para.GroupCode, para.Vfwebqq, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV1Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedGroupInfo>>(para.CookieContainer);
            return response.Result;
        }

        /// <summary>
        /// 获取单个讨论组详细信息
        /// </summary>
        public static ReturnedDisscussInfo LoadDisscussInfo(LoadDisscussInfoRequest para)
        {
            var url = string.Format(QrobotConsts.URL_DisscussInfo, para.DisscussId, para.Vfwebqq, para.ClientId, para.Psessionid, para.Milliscond);
            var request = new HttpRequestHandler(url);
            request.ApplyV2Referer(para.RefererV);
            var response = request.RequestJsonModel<BaseResponse<ReturnedDisscussInfo>>(para.CookieContainer);
            return response.Result;
        }

        /// <summary>
        /// 获取消息
        /// </summary>
        public static PollResponse PollMessage(PollRequest para)
        {
            var parameter = string.Format(QrobotConsts.PARA_PollMessage, para.Ptwebqq, para.ClientId, para.Psessionid);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(QrobotConsts.URL_PollMessage);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.Referer = QrobotConsts.URL_SendPollReferer;
            var response = request.RequestJsonModel<PollResponse>(para.CookieContainer);
            return response;
        }

        public static void SendMessage(SendMessageRequest para)
        {
            switch (para.Target.TargetType)
            {
                case ReplyTargetType.Personal:
                    SendPersonalMessage(para);
                    break;
                case ReplyTargetType.Group:
                    SendGroupMessage(para);
                    break;
                case ReplyTargetType.Disscuss:
                    SendDisscussMessage(para);
                    break;
            }
        }

        public static void SendPersonalMessage(SendMessageRequest sendPara)
        {
            PrivateSendMessage(QrobotConsts.URL_SendPersonalMessage, QrobotConsts.PARA_SendPersonalMessage, sendPara);
        }

        public static void SendGroupMessage(SendMessageRequest sendPara)
        {
            PrivateSendMessage(QrobotConsts.URL_SendGroupMessage, QrobotConsts.PARA_SendGroupMessage, sendPara);
        }

        public static void SendDisscussMessage(SendMessageRequest sendPara)
        {
            PrivateSendMessage(QrobotConsts.URL_SendDisscussMessage, QrobotConsts.PARA_SendDisscussMessage, sendPara);
        }

        private static void PrivateSendMessage(string url, string paraConst, SendMessageRequest sendPara)
        {
            var parameter = string.Format(paraConst, sendPara.Target.NumberOrName, sendPara.Content, sendPara.Face, sendPara.ClientId, sendPara.MessageId, sendPara.Psessionid);
            parameter = CommonUtils.ParaEncodingWithoutStart(parameter);
            var request = new HttpRequestHandler(url);
            request.Method = HttpRequestHandler.HttpMethod.Post;
            request.Parameters = parameter;
            request.Referer = QrobotConsts.URL_SendPollReferer;
            var response = request.RequestJsonModel<SendMessageResponse>(sendPara.CookieContainer);
            if (response.ErrorCode != 0)
            {
                // todo: 记录日志
            }
        }
    }
}
